package com.mdoya.controller;

import com.mdoya.dto.SetmealDto;
import com.mdoya.exception.BusinessExceptiion;
import com.mdoya.query.BaseQuery;
import com.mdoya.response.ResultResponse;
import com.mdoya.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     *  新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public ResultResponse add(@RequestBody SetmealDto setmealDto){
        log.info("新增套餐: {}",setmealDto);
        return ResultResponse.success(setmealService.insert(setmealDto) > 0 ? "新增套餐成功" : "新增套餐失败");
    }

    /**
     *  套餐分页查询
     * @param query
     * @return
     */
    @GetMapping("/page")
    public ResultResponse selectPage(BaseQuery query){
        log.info("套餐分页",query);
        return ResultResponse.success(setmealService.selectPage(query));
    }

    /**
     *  更新套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public ResultResponse updateById(@RequestBody SetmealDto setmealDto){
        log.info("更新套餐: {}",setmealDto);
        return ResultResponse.success(setmealService.updateById(setmealDto) > 0 ? "更新套餐成功" : "更新套餐失败");
    }
    @DeleteMapping
    public ResultResponse delete(@RequestParam List<Long> ids){
        log.info("删除套餐: {}",ids);
        return ResultResponse.success(setmealService.delete(ids) > 0 ? "删除套餐成功" : "删除套餐失败");
    }

    /**
     *   修改状态
     * @param map
     * @return
     */
    @PostMapping("/status")
    public ResultResponse status(@RequestBody Map<String,String> map){
        log.info("套餐销售状态: {}",map);
        String strIds = map.get("ids");
        List<String> ids = (List<String>) Arrays.asList(strIds.split(","));
        if (ids == null && ids.size() < 0){
            throw new BusinessExceptiion(-1,"请选择套餐");
        }
        String status = map.get("status");
        return ResultResponse.success(setmealService.status(ids,status) > 0 ? "操作套餐成功" : "操作套餐失败");
    }

    /**
     *  前端套餐查询
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public ResultResponse list(Long categoryId,Integer status){
        log.info("前端套餐查询: {}",categoryId," : ",status);
        return ResultResponse.success(setmealService.list(categoryId,status));
    }

}
